var fs = require("fs");
var path = require("path");
var es = require("event-stream");
var d = require("debug")("[test-event-stream.js]");
d.enabled = true;

var lineNr = 0;

var s = fs.createReadStream(
    path.join(__dirname, "../dev-mht/150829-150913.mht")
).pipe(es.split()).pipe(es.mapSync(function (line) {
    s.pause();
    lineNr++;

    LineProcess(line).then(() => {
        d(lineNr, line);
        s.resume();
    })
}));

function LineProcess(line) {
    return new Promise((r) => {
        let ms = parseInt(Math.random() * 10000) % 10 * 1000;
        setTimeout(() => {
            console.log("process..", line);
            r();
        }, ms);
    })
}